/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.thingsgrid.notice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thingsgrid.notice.entity.SendEntiy;
import com.thingsgrid.notice.exception.NoticeException;
import com.thingsgrid.notice.repository.ICommonRepository;
import com.thingsgrid.notice.repository.ISendRepository;
import com.thingsgrid.notice.service.ISendService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Slf4j
@AllArgsConstructor
@Service
public class SendServiceImpl implements ISendService<SendEntiy> {
    private ISendRepository iSendRepository;
    private ICommonRepository iCommonRepository;
    private final String MESSAGE_TEXT_ID = "message_text_id";
    private final String RECEIVER_NAME = "receiver_name";
    @Override
    public SendEntiy save(SendEntiy sendEntiy) {
        return iSendRepository.save(sendEntiy);
    }
    @Override
    public R readBatch(String ids) {
        try{
            List<String> listId = JSON.parseArray(ids,String.class);
            List<JSONObject> list = new ArrayList<>();
            listId.forEach(v->{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("status","1");
                jsonObject.put("update_time", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                list.add(jsonObject);
            });
            if(iCommonRepository.updateBatch(SendEntiy.class,MESSAGE_TEXT_ID,listId,list)) {
                return R.success("OK");
            } else {
                throw new NoticeException(R.fail("批量已阅失败"));
            }
        }catch (Exception e){
            throw new NoticeException(R.fail("批量已阅失败"));
        }
    }

    @Override
    public R readAll(String receiverName) {
        try{
            Map map = new HashMap(2);
            map.put("status","1");
            map.put("update_time",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
            if(iCommonRepository.updateBatch(SendEntiy.class,RECEIVER_NAME,receiverName,map)) {
                return R.success("OK");
            }else {
                throw new NoticeException(R.fail("全部已阅失败"));
            }
        }catch (Exception e){
            throw new NoticeException(R.fail("全部已阅失败"));
        }
    }

    @Override
    public List<SendEntiy> queryLikeByReceiverName(String findVal) {
        return iSendRepository.queryLikeByReceiverName(findVal);
    }

}
